﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Text;
using System.Collections.Generic;
using System.Data.SqlClient;
using FileConvertPdfSystemSQLHelper;
using FileConvertPdfSystemModel;

namespace FileConvertPdfSystemDAL
{
    public class UserInformation1
    {
        public int Add_UserInformation(UserInformation userinformation)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList ={ 
                 sqlHelper.CreateInParam("@UserNameLogin",SqlDbType.NVarChar,10,userinformation.UserNameLogin),
                 sqlHelper.CreateInParam("@Sex",SqlDbType.NChar,2,userinformation.Sex),
                 sqlHelper.CreateInParam("@Password",SqlDbType.NVarChar,10,userinformation.Password),
                 sqlHelper.CreateInParam("@TrueName",SqlDbType.NChar,10,userinformation.TrueName),
                 sqlHelper.CreateInParam("@Questions",SqlDbType.NChar,20,userinformation.Questions),
                 sqlHelper.CreateInParam("@Answers",SqlDbType.NChar,20,userinformation.Answers),
                 sqlHelper.CreateInParam("@Phonecode",SqlDbType.NChar,15,userinformation.Phonecode),
                 sqlHelper.CreateInParam("@Emails",SqlDbType.NChar,30,userinformation.Emails),
                 sqlHelper.CreateInParam("@City",SqlDbType.NChar,10,userinformation.City),
                 sqlHelper.CreateInParam("@Address",SqlDbType.NChar,50,userinformation.Address),
                 sqlHelper.CreateInParam("@PostCode",SqlDbType.NChar,10,userinformation.PostCode),
                 sqlHelper.CreateInParam("@score",SqlDbType.Int,4,userinformation.Score),
                 sqlHelper.CreateInParam("@LastLoadTime",SqlDbType.DateTime,8,userinformation.LoadTime),
                 sqlHelper.CreateInParam("@LoadDate",SqlDbType.DateTime,8,userinformation.LastLoginTime)
             };
            try
            {
                return (sqlHelper.RunProc("Add_User", ParamList));
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }

        }
        public void Update_UserInformation(UserInformation userinformation)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList ={ 
                 sqlHelper.CreateInParam("@ID",SqlDbType.Int,4,userinformation.ID),
                 sqlHelper.CreateInParam("@UserNameLogin",SqlDbType.NVarChar,10,userinformation.UserNameLogin),
                 sqlHelper.CreateInParam("@Sex",SqlDbType.NVarChar,2,userinformation.Sex),
                 sqlHelper.CreateInParam("@Password",SqlDbType.NVarChar,10,userinformation.Password),
                 sqlHelper.CreateInParam("@TrueName",SqlDbType.NVarChar,10,userinformation.TrueName),
                 sqlHelper.CreateInParam("@Questions",SqlDbType.NVarChar,20,userinformation.Questions),
                 sqlHelper.CreateInParam("@Answers",SqlDbType.NVarChar,20,userinformation.Answers),
                 sqlHelper.CreateInParam("@Phonecode",SqlDbType.NVarChar,15,userinformation.Phonecode),
                 sqlHelper.CreateInParam("@Emails",SqlDbType.NVarChar,30,userinformation.Emails),
                 sqlHelper.CreateInParam("@City",SqlDbType.NVarChar,10,userinformation.City),
                 sqlHelper.CreateInParam("@Address",SqlDbType.NVarChar,50,userinformation.Address),
                 sqlHelper.CreateInParam("@PostCode",SqlDbType.NVarChar,10,userinformation.PostCode),
                 sqlHelper.CreateInParam("@score",SqlDbType.Int,4,userinformation.Score),
                 sqlHelper.CreateInParam("@LastLoadTime",SqlDbType.DateTime,8,userinformation.LoadTime),
                 sqlHelper.CreateInParam("@LoadDate",SqlDbType.DateTime,8,userinformation.LastLoginTime)
             };
            try
            {
                sqlHelper.RunProc("Update_UserInformation", ParamList);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }
        public void Delete_UserInformation(int nID)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList = { sqlHelper.CreateInParam("@ID", SqlDbType.Int, 4, nID) };
            try
            {
                sqlHelper.RunProc("Delete_UserInformation", ParamList);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
        }
        public List<UserInformation> Get_UserInformation()
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlDataReader rec = null;
            try
            {
                sqlHelper.RunProc("Get_UserInformation", out rec);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
            List<UserInformation> List_UserInformation = new List<UserInformation>();
            while (rec.Read())
            {
                UserInformation Userinfor = new UserInformation();
                Userinfor.ID = Int32.Parse(rec["id"].ToString());
                Userinfor.UserNameLogin = rec["UserNameLogin"].ToString();
                Userinfor.Sex = rec["Sex"].ToString();
                Userinfor.Password = rec["Password"].ToString();
                Userinfor.TrueName = rec["TrueName"].ToString();
                Userinfor.Questions = rec["Questions"].ToString();
                Userinfor.Answers = rec["Answers"].ToString();
                Userinfor.Phonecode = rec["Phonecode"].ToString();
                Userinfor.Emails = rec["Emails"].ToString();
                Userinfor.City = rec["City"].ToString();
                Userinfor.Address = rec["Address"].ToString();
                Userinfor.PostCode = rec["PostCode"].ToString();
                Userinfor.Score = Convert.ToInt32(rec["score"].ToString());
                Userinfor.LoadTime = DateTime.Parse(rec["LastLoadTime"].ToString());
                Userinfor.LastLoginTime = DateTime.Parse(rec["LoadDate"].ToString());

                List_UserInformation.Add(Userinfor);
                Userinfor = null;
            }
            return List_UserInformation;
        }
        public UserInformation Get_SingUserInformation(string sUserName)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList = { sqlHelper.CreateInParam("@UserNameLogin", SqlDbType.NVarChar, 10, sUserName) };
            SqlDataReader rec = null;
            try
            {
                sqlHelper.RunProc("Get_SingUserInformation", ParamList, out rec);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
            UserInformation Userinfor = new UserInformation();
            while (rec.Read())
            {
                Userinfor.ID = Int32.Parse(rec["ID"].ToString());
                Userinfor.UserNameLogin = rec["UserNameLogin"].ToString();
                Userinfor.Sex = rec["Sex"].ToString();
                Userinfor.Password = rec["Password"].ToString();
                Userinfor.TrueName = rec["TrueName"].ToString();
                Userinfor.Questions = rec["Questions"].ToString();
                Userinfor.Answers = rec["Answers"].ToString();
                Userinfor.Phonecode = rec["Phonecode"].ToString();
                Userinfor.Emails = rec["Emails"].ToString();
                Userinfor.City = rec["City"].ToString();
                Userinfor.Address = rec["City"].ToString();
                Userinfor.PostCode = rec["PostCode"].ToString();
                Userinfor.Score = Convert.ToInt32(rec["score"].ToString());
                Userinfor.LoadTime = DateTime.Parse(rec["LastLoadTime"].ToString());
                Userinfor.LastLoginTime = DateTime.Parse(rec["LoadDate"].ToString());

            }
            return Userinfor;
        }
        public bool Login_UserInformation(string sUserName)
        {
            FileConvertPdfSystemSQLHelper.SQLHelper sqlHelper = new FileConvertPdfSystemSQLHelper.SQLHelper();
            SqlParameter[] ParamList = { sqlHelper.CreateInParam("@UserNameLogin", SqlDbType.NVarChar, 10, sUserName) };
            SqlDataReader rec = null;
            try
            {
                sqlHelper.RunProc("Login_Admin", ParamList, out rec);
            }
            catch (Exception ex)
            {
                SystemError.CreateErrorLog(ex.Message);
                throw new Exception(ex.Message, ex);
            }
            bool Bool = false;
            while (rec.Read())
            {
                if (sUserName == rec["UserNameLogin"].ToString())
                {
                    Bool = true;
                }
            }
            return Bool;
        }

    }
}
